package org.winexpress

import org.codehaus.groovy.grails.web.json.JSONObject;
import org.codehaus.groovy.grails.web.json.JSONArray;
import grails.converters.JSON

class WineVarietalController {

    // REST operations
    // GET
    def show = {
        println 'WineVarietalController >> Show >> START'
        
        WineVarietalManagement wvman = new WineVarietalManagement()
        
        if (params.id && WineVarietal.exists(params.id)) {    
            def wv = wvman.getWineVarietalByID(params.id)
            render wv as JSON
            
            println 'Show wine varietal by ID >> ' + wv.id + " " + wv
        }
        else {
            def allWV = wvman.listWineVarietals()
            if (allWV.length() > 0) {
                render allWV as JSON
            }
            else {
                JSONObject o = new JSONObject()
                o.put("404",true)
                render o as JSON
            } 
            
            println 'Listing all wine cats >> ' + allWV
        }
        
        println 'WineVarietalController >> Show >> END'
    }
    
    // PUT
    def update = {
        println 'WineVarietalController >> Update >> START'
        
        JSONObject o = request.JSON
        
        WineVarietalManagement wvman = new WineVarietalManagement()
        
        def wv = wvman.getWineVarietalByID(o.get("id"))
        if (o.has("varietalName")) { wv.varietalName = o.get("varietalName"); println wv.varietalName }
        if (o.has("description")) { wv.description = o.get("description"); println wv.description }
        if (o.has("wineType")) { 
            WineTypeManagement wtman = new WineTypeManagement()
            def wt = wtman.getWineTypeForDbByTypeName(o.get("wineType"))
            wv.wineType = wt
            println wv.wineType 
        }
        
        def result = wvman.updateWineVarietal(wv)
        if (result == true) {
            println 'WineVarietalController >> Update >> ' + result
//            render response as JSON
        }
        else {
            println 'WineVarietalController >> Update >> ' + result
        }
        

        println 'WineVarietalController >> Update >> END'

    }
    
    // DELETE
    def delete = {
        println 'WineVarietalController >> delete >> START'
        
        WineVarietalManagement wvman = new WineVarietalManagement()
        
        if (params.id && WineVarietal.exists(params.id)) {    
            def result = wvman.deleteWineVarietal(id)
            
            if (result == true) {
            println 'WineVarietalController >> delete >> ' + result
    //            render response as JSON
            }
            else {
                println 'WineVarietalController >> delete >> ' + result
            }
        }
        
        println 'WineVarietalController >> delete >> END'
    }
    
    // POST
    def save = {
        println 'WineVarietalController >> Save >> START'
        
        JSONObject o = request.JSON
        
        WineVarietalManagement wvman = new WineVarietalManagement()
        
        def result = wvman.addWineVarietal(o)
        if (result == true) {
            println 'WineVarietalController >> Save >> ' + result
//            render response as JSON
        }
        else {
            println 'WineVarietalController >> Save >> ' + result
        }
        
        println 'WineVarietalController >> Save >> END'
    }
}
